package com.fxust.thread;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import com.fxust.common.SqlMapper;
import com.fxust.dao.ThirdTableDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Class Desc
 *
 * @author johnking
 * @create 2017-12-19 下午11:18
 **/

public class DealTextFileThread extends Thread {

    private static Logger logger = LoggerFactory.getLogger(DealTextFileThread.class);

    private String uuid;
    private String filePath;

    public DealTextFileThread(String uuid, String filePath) {
        this.uuid = uuid;
        this.filePath = filePath;
    }

    @Override
    public void run() {
        int count = readTextContent();
        logger.info("处理数据总量:"+count);
    }

    private int readTextContent() {
        File file = new File(filePath);
        BufferedReader reader = null;
        String line = "";
        int rowCount = 0;
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
            while ((line = reader.readLine()) != null) {
                insertTextToDb(line);
                rowCount++;
            }

        } catch (IOException e) {
            logger.error("文件读取错误");
        }
        return rowCount;
    }

    private void insertTextToDb(String param){
        String[] textData = param.split(",");
        try{

            logger.info("数据长度:"+textData.length);
            String insertSql = SqlMapper.insertSql;
            ThirdTableDao.executeInsertSQL(textData, insertSql);
        }catch(Exception e){
            logger.error("执行插入错误:"+textData.toString(),e);
        }

    }

}
